﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Kevin.SLUserControlLibrary
{
    public class ColorPickerComboBox : ComboBox
    {
        public static readonly DependencyProperty SelectedColorProperty = DependencyProperty
            .Register("SelectedColor", typeof(SolidColorBrush), typeof(ColorPickerComboBox),
            new PropertyMetadata(new PropertyChangedCallback(ColorPickerComboBox.OnSelectedColorChanged)));

        private ScrollViewer ElementScrollViewer;
        private Border ElementPopupBorder;
        private ContentPresenter ElementContentPresenter;
        private ColorPickerDropDown ElementDropDown;
        private ColorItem ElementSelectedColorItem;
        private ColorPickerSelectedItem ElementColorPickerSelectedItem;

        public SolidColorBrush SelectedColor
        {
            get { return (SolidColorBrush)GetValue(ColorPickerComboBox.SelectedColorProperty); }
            set { this.SetValue(ColorPickerComboBox.SelectedColorProperty, value); }
        }

        public ColorPickerComboBox()
        {
            base.DefaultStyleKey = typeof(ComboBox);
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            this.ElementScrollViewer = base.GetTemplateChild("ScrollViewer") as ScrollViewer;
            this.ElementPopupBorder = base.GetTemplateChild("PopupBorder") as Border;
            this.ElementContentPresenter = base.GetTemplateChild("ContentPresenter") as ContentPresenter;

            this.ElementDropDown = new ColorPickerDropDown();
            this.ElementScrollViewer.Content = ElementDropDown;
            this.ElementDropDown.DisplayColor = this.SelectedColor;
            this.ElementDropDown.OnSelectComplete += new ColorPickerDropDown.SelectComplete(ElementDropDown_OnSelectComplete);

            this.ElementPopupBorder.BorderThickness = new Thickness(0);
            this.ElementPopupBorder.Background = new SolidColorBrush(Colors.Transparent);
            this.ElementContentPresenter.Visibility = System.Windows.Visibility.Collapsed;

            this.ElementColorPickerSelectedItem = new ColorPickerSelectedItem();
            this.ElementColorPickerSelectedItem.DisplayColor = this.SelectedColor;
            this.ElementColorPickerSelectedItem.Margin = new Thickness(10, 0, 0, 0);
            (this.ElementContentPresenter.Parent as Grid).Children.Add(this.ElementColorPickerSelectedItem);
        }


        private void ElementDropDown_OnSelectComplete(ColorItem colorItem, SolidColorBrush selectedColor)
        {
            this.SelectedColor = selectedColor;

            if (this.ElementSelectedColorItem != null)
                this.ElementSelectedColorItem.IsChecked = false;

            this.ElementSelectedColorItem = colorItem;
            this.IsDropDownOpen = false;
        }

        private static void OnSelectedColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ColorPickerComboBox colorPickerComboBox = (ColorPickerComboBox)d;

            if (colorPickerComboBox.ElementColorPickerSelectedItem != null)
                colorPickerComboBox.ElementColorPickerSelectedItem.DisplayColor = colorPickerComboBox.SelectedColor;

            if (colorPickerComboBox.ElementDropDown != null)
                colorPickerComboBox.ElementDropDown.DisplayColor = colorPickerComboBox.SelectedColor;
        }

    }
}
